#include "legendeditdialog.h"
#include "ui_legendeditdialog.h"
#include "abstractplot2d.h"
#include "legendmodel.h"
#include "legenddelegate.h"
#include "greekalphabetdialog.h"
#include <QFontDialog>

LegendEditDialog::LegendEditDialog(AbstractPlot2D *plotter, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LegendEditDialog),
    _plotter(plotter)
{
    ui->setupUi(this);

    QStringList _legend;
    foreach (CurveData cd, _plotter->curvesList())
        _legend.append(cd.legend());

    model = new LegendModel(_legend, this);

    ui->treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->treeView->setModel(model);
    ui->treeView->setItemDelegateForColumn(0, new LegendDelegate(this));
    ui->treeView->setCurrentIndex(model->index(0,0));

    init();

    connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(changeText(QString)));
    connect(ui->supBtn, SIGNAL(clicked()), this, SLOT(addSupIndex()));
    connect(ui->subBtn, SIGNAL(clicked()), this, SLOT(addSubIndex()));
    connect(ui->boldBtn, SIGNAL(clicked()), this, SLOT(addBold()));
    connect(ui->itBtn, SIGNAL(clicked()), this, SLOT(addItalic()));
    connect(ui->uBtn, SIGNAL(clicked()), this, SLOT(addUnderline()));
    connect(ui->greekAlpBtn, SIGNAL(clicked()), this, SLOT(showGreekAlphabetDlg()));

    connect(ui->fontBtn, SIGNAL(clicked()), this, SLOT(changeFont()));
    connect(ui->treeView, SIGNAL(activated(QModelIndex)), this, SLOT(changeCurve(QModelIndex)));
    connect(ui->applyBtn, SIGNAL(clicked()), this, SLOT(apply()));

    greekAlphabetDlg = 0;
}
//-----------------------------------------------------------------------------

LegendEditDialog::~LegendEditDialog()
{
    delete ui;
}
//-----------------------------------------------------------------------------

void LegendEditDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
//-----------------------------------------------------------------------------

void LegendEditDialog::init()
{
    ui->spinBox->setValue(_plotter->legendOpacity());
    _font = _plotter->legendFont();
    ui->frameCheckBox->setChecked(_plotter->legendFrameIsShow());
    QModelIndex index = ui->treeView->currentIndex();
    ui->lineEdit->setText(model->data(index, Qt::EditRole).toString());
    ui->previewLabel->setText(model->data(index, Qt::EditRole).toString());
    ui->previewLabel->setFont(_font);
}
//-----------------------------------------------------------------------------

void LegendEditDialog::apply()
{
    _plotter->setLegendOpacity(ui->spinBox->value());
    _plotter->showLegendFrame(ui->frameCheckBox->isChecked());
    _plotter->setLegendFont(_font);
    QStringList sl = model->stringList();
    for (int i = 0; i < sl.size(); ++i) {
        _plotter->curvesList()[i].setLegend(sl.at(i));
    }
    _plotter->updatePlot();
}
//-----------------------------------------------------------------------------

void LegendEditDialog::changeFont()
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok, _font, this);
    if (ok) {
        _font = font;
        ui->previewLabel->setFont(_font);
    }
}
//-----------------------------------------------------------------------------

void LegendEditDialog::addSupIndex()
{
    int i = ui->lineEdit->cursorPosition();
    QString str = "<sup></sup>";
    QString curStr = ui->lineEdit->text();
    curStr.insert(i, str);
    ui->lineEdit->setText(curStr);
    i += 5;
    ui->lineEdit->setCursorPosition(i);
}
//-----------------------------------------------------------------------------

void LegendEditDialog::addSubIndex()
{
    int i = ui->lineEdit->cursorPosition();
    QString str = "<sub></sub>";
    QString curStr = ui->lineEdit->text();
    curStr.insert(i, str);
    ui->lineEdit->setText(curStr);
    i += 5;
    ui->lineEdit->setCursorPosition(i);
}
//-----------------------------------------------------------------------------

void LegendEditDialog::addBold()
{
    int i = ui->lineEdit->cursorPosition();
    QString str = "<b></b>";
    QString curStr = ui->lineEdit->text();
    curStr.insert(i, str);
    ui->lineEdit->setText(curStr);
    i += 3;
    ui->lineEdit->setCursorPosition(i);
}
//-----------------------------------------------------------------------------

void LegendEditDialog::addItalic()
{
    int i = ui->lineEdit->cursorPosition();
    QString str = "<i></i>";
    QString curStr = ui->lineEdit->text();
    curStr.insert(i, str);
    ui->lineEdit->setText(curStr);
    i += 3;
    ui->lineEdit->setCursorPosition(i);
}
//-----------------------------------------------------------------------------

void LegendEditDialog::addUnderline()
{
    int i = ui->lineEdit->cursorPosition();
    QString str = "<u></u>";
    QString curStr = ui->lineEdit->text();
    curStr.insert(i, str);
    ui->lineEdit->setText(curStr);
    i += 3;
    ui->lineEdit->setCursorPosition(i);
}
//-----------------------------------------------------------------------------

void LegendEditDialog::showGreekAlphabetDlg()
{
    if (!greekAlphabetDlg) {
        greekAlphabetDlg = new GreekAlphabetDialog(this);
        connect(greekAlphabetDlg, SIGNAL(addSymbol(QString)), this, SLOT(addGreekSymbl(QString)));
    }
    greekAlphabetDlg->show();
}
//-----------------------------------------------------------------------------

void LegendEditDialog::addGreekSymbl(const QString &str)
{
    int i = ui->lineEdit->cursorPosition();
    QString curStr = ui->lineEdit->text();
    curStr.insert(i, str);
    ui->lineEdit->setText(curStr);
    i += str.size();
    ui->lineEdit->setCursorPosition(i);
}
//-----------------------------------------------------------------------------

void LegendEditDialog::changeText(const QString &text)
{
    ui->previewLabel->setText(text);
    QModelIndex index = ui->treeView->currentIndex();
    model->setData(index, text, Qt::EditRole);
}
//-----------------------------------------------------------------------------

void LegendEditDialog::changeCurve(const QModelIndex &index)
{
    ui->lineEdit->setText(model->data(index, Qt::EditRole).toString());
}
//-----------------------------------------------------------------------------
